var homelibURL = "homeLib.php" ;

// function that displays an error message
function displayError(message)
{
    // display error message, with more technical details if debugMode is true
    alert("Error accessing the server! " +
        (debugMode ? message : ""));
}

// function that displays a PHP error message
function displayPHPError(error)
{
    displayError ("Error number :" + error.errno + "\r\n" +
        "Text :"+ error.text + "\r\n" +
        "Location :" + error.location + "\r\n" +
        "Line :" + error.line + + "\r\n");
}

function retrieveNewNews()
{
    $.ajax({
        url: homelibURL,
        type: 'POST',
        data: $.param({
            mode: 'RetrieveNew'
        }),
        dataType: 'json',
        error: function(xhr, textStatus, errorThrown) {
            displayError(textStatus);
        },
        success: function(data, textStatus) {
            if(data.errno != null)
                displayPHPError(data);
            else
                readMessages(data);
            // restart sequence
            setTimeout("retrieveNewNews();", updateInterval);
        }
    });
}

function retrieveLogos()
{
    $.ajax({
        url: homelibURL,
        type: 'POST',
        data: $.param({
            mode: 'RetrieveLogo'
        }),
        dataType: 'json',
        error: function(xhr, textStatus, errorThrown) {
            displayError(textStatus);
        },
        success: function(data, textStatus) {
            if(data.errno != null)
                displayPHPError(data);
            else
                logo(data);
            // restart sequence
            setTimeout("retrieveLogos();", updateInterval);
        }
    });
}

function retrieveStages()
{
    $.ajax({
        url: homelibURL,
        type: 'POST',
        data: $.param({
            mode: 'RetrieveStage'
        }),
        dataType: 'json',
        error: function(xhr, textStatus, errorThrown) {
            displayError(textStatus);
        },
        success: function(data, textStatus) {
            if(data.errno != null)
                displayPHPError(data);
            else
                stage(data);
            // restart sequence
            setTimeout("retrieveStages();", updateInterval);
        }
    });
}


function retrieveVacatures()
{
    $.ajax({
        url: homelibURL,
        type: 'POST',
        data: $.param({
            mode: 'RetrieveVacatures'
        }),
        dataType: 'json',
        error: function(xhr, textStatus, errorThrown) {
            displayError(textStatus);
        },
        success: function(data, textStatus) {
            if(data.errno != null)
                displayPHPError(data);
            else
                vacatures(data);
            // restart sequence
            setTimeout("retrieveVacatures();", updateInterval);
        }
    });
}

function retrieveAllNews()
{
    $.ajax({
        url: homelibURL,
        type: 'POST',
        data: $.param({
            mode: 'RetrieveAllNew'
        }),
        dataType: 'json',
        error: function(xhr, textStatus, errorThrown) {
            displayError(textStatus);
        },
        success: function(data, textStatus) {
            if(data.errno != null)
                displayPHPError(data);
            else
                readAllMessages(data);
            // restart sequence
            setTimeout("retrieveAllNews();", updateInterval);
        }
    });
}

function retrieveAllTopics()
{
    $.ajax({
        url: homelibURL,
        type: 'POST',
        data: $.param({
            mode: 'RetrieveAllTopics'
        }),
        dataType: 'json',
        error: function(xhr, textStatus, errorThrown) {
            displayError(textStatus);
        },
        success: function(data, textStatus) {
            if(data.errno != null)
                displayPHPError(data);
            else
                readAllTopics(data);
            // restart sequence
            setTimeout("retrieveAllTopics();", updateInterval);
        }
    });
}

function readMessages(data, textStatus)
{
    // display the messages retrieved from server
    $.each(data.messages, function(i, message) {
        // compose the HTML code that displays the message
        var htmlMessage = "";
        htmlMessage += "<h4>" + message.titel + "</h4><hr>";
        htmlMessage += "<p>"+message.tekst+"</p>";
        htmlMessage += "<h5>Posted on: "+message.datum+"</h5>";
        // display the message
        $('#nieuws')[0].innerHTML += htmlMessage;
    });
}

function logo(data, textStatus)
{
    // display the messages retrieved from server
    $.each(data.messages, function(i, message) {
        // compose the HTML code that displays the message
        var htmlMessage = "";
        if (message.logo!='') {
            htmlMessage += "<img src=\"images/fotos/" + message.logo + "\" title=\""+ message.instellingnaam +"\"/>";
        }
        // display the message
        $('#logos')[0].innerHTML += htmlMessage;
    });
}

function stage(data, textStatus)
{
    // display the messages retrieved from server
    $.each(data.messages, function(i, message) {
        // compose the HTML code that displays the message
        var htmlMessage = "";
        if(message.stagetitel!=''){
            htmlMessage += "<p>" + message.stagetitel + " in "+ message.stageplaats +"</p>";
        }
        // display the message
        $('#stages')[0].innerHTML += htmlMessage;
    });
}


function vacatures(data, textStatus)
{
    // display the messages retrieved from server
    $.each(data.messages, function(i, message) {
        // compose the HTML code that displays the message
        var htmlMessage = "";
        if(message.stagetitel!=''){
            htmlMessage += "<p>" + message.vacaturetitel + " in "+ message.vacaturplaats +"</p>";
        }
        // display the message
        $('#vacatures')[0].innerHTML += htmlMessage;
    });
}

function readAllMessages(data, textStatus)
{
    // display the messages retrieved from server
    $.each(data.messages, function(i, message) {
        // compose the HTML code that displays the message
        var htmlMessage = "";
        htmlMessage += "<a name=\""+message.id_nieuws+"\"></a>";
        htmlMessage += "<h4>" + message.titel + "</h4><hr>";
        htmlMessage += "<p>"+message.tekst+"</p>";
        if(message.link!=''){
            htmlMessage += "<h5>Link:<a href=\""+message.link+"\" target=\"blank\">"+message.link+"</a></h5>";
        }
        htmlMessage += "<h5>Posted on: "+message.datum+"</h5>";
        // display the message
        $('#allnieuws')[0].innerHTML += htmlMessage;

        var TitelMessage = "";
        TitelMessage += "<li><a href=\"#"+message.id_nieuws+"\">"+message.titel+"</a></li>";
        // display the message
        $('#sidebarNews')[0].innerHTML += TitelMessage;
    });
}

function readAllTopics(data, textStatus)
{
    // display the messages retrieved from server
    $.each(data.messages, function(i, message) {
        // compose the HTML code that displays the message
        var htmlMessage = "";
        htmlMessage += "<form action=\"index.php?p=topic\" method=\"post\">";
        htmlMessage += "<input type=\"hidden\" name=\"topicID\" value=\""+message.topicID+"\" />";
        htmlMessage += "<input type=\"hidden\" name=\"relatieNummer\" value=\""+message.relatieNummer+"\" />";

        htmlMessage += message.topicTitel+"</a> geplaast op "+message.datum + ", 0 reactie";
        htmlMessage += " <input type=\"submit\" name=\"submit\" value=\"View\">";
        htmlMessage += "</form>";

        // display the message
        $('#topics')[0].innerHTML += htmlMessage;
    });
}


$(document).ready(function()
{
    retrieveNewNews();
    retrieveLogos();
    retrieveStages();
    retrieveAllNews();
    retrieveVacatures();
    retrieveAllTopics();
});